<?php

use admin\assets\PageAsset;
use yii\jui\JuiAsset;
use yii\helpers\Url;
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use ijony\admin\widgets\ActiveField;

/* @var $this yii\web\View */
/* @var $model admin\models\Cases */
/* @var $form yii\bootstrap\ActiveForm  */

PageAsset::register($this)->init([
    'js' => [
        'js/laytpl.js',
        'js/jquery.uploadifive.js',
    ],
]);

JuiAsset::register($this);
?>

<div class="ibox">
    <div class="ibox-content">

        <?php $form = ActiveForm::begin([
            'fieldClass' => ActiveField::className(),
            'layout' => 'horizontal',
            'fieldConfig' => [
                'inline' => true,
                'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
                'horizontalCssClasses' => [
                    'label' => 'col-sm-2',
                    'offset' => 'col-sm-offset-2',
                    'wrapper' => 'col-sm-10',
                    'error' => '',
                    'hint' => '',
                ],
            ],
        ]); ?>

        <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
        <?= $form->field($model, 'case_id')->chosen($model->getCaseSelectData()) ?>
        <?= $form->field($model, 'content')->editor() ?>
        <?= $form->field($model, 'status')->radioList($model->getStatusSelectData()) ?>

        <div class="form-group">
            <div class="col-sm-4 col-sm-offset-2">
                <?= Html::resetButton('重置', ['class' => 'btn btn-white']) ?>
                <?= Html::submitButton('保存', ['class' => 'btn btn-primary']) ?>
            </div>
        </div>

        <?php ActiveForm::end(); ?>

    </div>
</div>

<?php

$csrfToken = Yii::$app->request->getCsrfToken();
$csrfName = Yii::$app->request->csrfParam;
$caseDropDown = Html::getInputId($model, 'case_id');
$caseDataUrl = Url::to(['ajax/filter-case']);

$js = <<<JS

$('#filterCase').click(function(){
    let keyword = $('#filterKey').val();
    
    $('#$caseDropDown option:first').nextAll().remove();
    
    if(keyword){
        $.post('$caseDataUrl', {'$csrfName': '$csrfToken', 'keyword': keyword}, function(data){
            if(data.html){
                $('#$caseDropDown').html(data.html);
            }
        }, 'json');
    }
});

JS;

$this->registerJs($js);